package com.zengqingfa.springboot.validation.demo;

import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.ChildBeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ScannedGenericBeanDefinition;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        // springboot启动
        // spring boot启动的时候会加载很多系统需要的一些bean
        ConfigurableApplicationContext run = SpringApplication.run(Application.class, args);

        ConfigurableListableBeanFactory beanFactory = run.getBeanFactory();
        Map<String, RootBeanDefinition> rootBeanDefinitionMap = new HashMap<>();
        Map<String, ScannedGenericBeanDefinition> scannedGenericBeanDefinitionMap = new HashMap<>();
        Map<String, AnnotatedBeanDefinition> annotatedBeanDefinitionMap = new HashMap<>();
        Map<String, ChildBeanDefinition> childBeanDefinitionMap = new HashMap<>();
        Map<String, GenericBeanDefinition> genericBeanDefinitionMap = new HashMap<>();


        List<String> configurationNames = new ArrayList<>();

//        Map<String, ConfigurationClassBeanDefinitionReader.ConfigurationClassBeanDefinition> configurationClassBeanDefinitionMap = new HashMap<>();
        int count = 0;
        if (beanFactory instanceof DefaultListableBeanFactory) {
            DefaultListableBeanFactory factory = (DefaultListableBeanFactory) beanFactory;
            String[] beanDefinitionNames = factory.getBeanDefinitionNames();
            for (String beanName : beanDefinitionNames) {
                BeanDefinition beanDefinition = factory.getBeanDefinition(beanName);
                if (beanDefinition instanceof AnnotatedBeanDefinition) {
                    count++;
                    annotatedBeanDefinitionMap.put(beanName, (AnnotatedBeanDefinition) beanDefinition);
                } else if (beanDefinition instanceof ScannedGenericBeanDefinition) {
                    count++;
                    scannedGenericBeanDefinitionMap.put(beanName, (ScannedGenericBeanDefinition) beanDefinition);
                } else if (beanDefinition instanceof GenericBeanDefinition) {
                    count++;
                    genericBeanDefinitionMap.put(beanName, (GenericBeanDefinition) beanDefinition);
                } else if (beanDefinition instanceof ChildBeanDefinition) {
                    count++;
                    childBeanDefinitionMap.put(beanName, (ChildBeanDefinition) beanDefinition);
                } else if (beanDefinition instanceof RootBeanDefinition) {
                    count++;
                    rootBeanDefinitionMap.put(beanName, (RootBeanDefinition) beanDefinition);
                }
            }
            System.out.println(count);
//            System.out.println(beanDefinitionNames.length);
//            BeanDefinition userServiceBeanDefinition = factory.getBeanDefinition("userService");
//            System.out.println(userServiceBeanDefinition);
//
//            BeanDefinition shitData = factory.getBeanDefinition("shitData");
//            System.out.println(shitData);
        }

//        String[] beanNamesForType = beanFactory.getBeanNamesForType(BeanPostProcessor.class);
//        System.out.println(beanNamesForType);
//        beanFactory
//        beanFactory.getTypeForFactoryBean

    }


}
